<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>


    <script src="https://s3.pstatp.com/cdn/expire-1-M/jquery/3.3.1/jquery.min.js"></script>
    <script src="/js/vue.js" type="text/javascript" charset="utf-8"></script>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <!-- 引入组件库 -->
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <!-- 引入axios-->
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>


</head>
<body>
<div id="app">
    <el-button type="success"  size="mini" @click="open" plain>新增秒杀</el-button>

    <el-dialog
            title="添加秒杀商品"
            :visible.sync="dialogVisible"
            width="30%"
            :before-close="handleClose">

        <el-form ref="form" v-model="form" label-width="80px">
            <el-form-item label="商品类别">
                <el-select v-model="product.category"  placeholder="请选择商品类别" v-on:change="change($event)">
                    <el-option v-for="productCategory in form.productCategorys" v-bind:label="productCategory.productTypeName"  v-bind:value="productCategory.productTypeId" ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="商品名字">
                <el-select v-model="product.name"  placeholder="请选择商品名字" >
                    <el-option v-for="productName in form.productNames" v-bind:label="productName.productName"  v-bind:value="productName.productId"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="活动时间">
                <el-col :span="11">
                    <el-date-picker type="date" value-format="yyyy-MM-dd" placeholder="选择日期"  v-model="product.date1" style="width: 100%;"></el-date-picker>
                </el-col>
                <el-col class="line" :span="2">-</el-col>
                <el-col :span="11">
                    <el-time-picker placeholder="选择时间" value-format="HH:mm:ss" v-model="product.date2" style="width: 100%;"></el-time-picker>
                </el-col>
            </el-form-item>

            <el-form-item label="持续时间">
                <el-select v-model="product.time"  placeholder="请选择时间" >
                    <el-option label="半小时" value="0.5"></el-option>
                    <el-option label="1小时" value="1"></el-option>
                    <el-option label="2小时" value="2"></el-option>
                    <el-option label="一天" value="24"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="商品数量">
                <el-input type="number" min="1" :plain="true" placeholder="请输入内容" v-model="product.number" @blur="checkNum(product.number)" clearable>
                </el-input>
            </el-form-item>
            <el-form-item label="商品折扣">
                <el-select v-model="product.discount"  placeholder="请选择折扣" >
                    <el-option label="98折" value="0.98"></el-option>
                    <el-option label="95折" value="0.95"></el-option>
                    <el-option label="9折" value="0.9"></el-option>
                    <el-option label="88折" value="0.88"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="success" @click="onSubmit">立即添加</el-button>
                <el-button type="info" @click="close">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
</div>
<script>
    const app=new Vue({
        el:"#app",
        methods:{
            onSubmit() {
                let p=app.product;
                let date=p.date1+" "+p.date2;
                let registerProduct={productId:p.name,date:date,time:p.time,number:p.number,discount:p.discount,productName:p.category}
                axios.post("/freshmarket/limited-time-offer/registerKillProduct",registerProduct)


                    .then(function (resp) {


                      if(resp.data.data==true){
                          app.dialogVisible=false;
                          app.product.category='';
                          app.product. name='';
                          app.product. date1='';
                          app.product.date2= '';
                          app.product.time='';
                          app.product.number='';
                          app.product.discount='';
                          app.$message({
                              message: "添加成功",
                              type: 'success'
                          });
                          return ;
                      }
                        app.$message({
                              message: "添加失败"+resp.data.data,
                              type: 'warning'
                          });

                    })
            },
            handleClose(done) {
                this.$confirm('确认关闭？')
                    .then(_ => {
                        done();
                    })
                    .catch(_ => {});
            }	,
            change(e){

                axios.get("/freshmarket/limited-time-offer/findByProductTypeId",{
                    params:{id:e}
                })
                    .then(function (resp) {
                        app.form.productNames=resp.data.data
                    })
            },


            checkNum(e){
                axios.get("/freshmarket/limited-time-offer/numbers",{
                    params:{id:app.product.name==''?0:app.product.name,numbers:e==''?0:e}
                })
                    .then(function (resp) {
                           if(resp.data.data!=null){
                           app.$message({
                               message: resp.data.data,
                               type: 'warning'
                           });
                           }
                    })
            },
            open(){
                app.dialogVisible = true;
                axios.get("/freshmarket/limited-time-offer/findNames")
                    .then(function (resp) {
                        app.form.productCategorys=resp.data.data
                    })
            },
            close(){
                app.dialogVisible = false;

                app.product.category='';
                app.product. name='';
                app.product. date1='';
                app.product.date2= '';
                app.product.time='';
                app.product.number='';
                app.product.discount='';

            }
        },

        data(){
            return {
                dialogVisible: false,
                form:{
                    productCategorys:[],
                    productNames:[],
                    date1: '',
                    date2: '',
                    time:'',
                    number: '',
                    discount:''
                },
                product:{
                    category:'',
                    name:'',
                    date1: '',
                    date2: '',
                    time:'',
                    number: '',
                    discount:''
                }
            };
        }

    })
</script>
</body>
</html>
